<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>基于OpenCV和C++车道线检测 | 啊粥啊周舟の部落阁</title><meta name="author" content="Zhouwy"><meta name="copyright" content="Zhouwy"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="基于OpenCV和C++车道线检前言： 这篇文章主要是一种基于边缘检测的传统车道线检测算法，这种算法完全依赖于OpenCV，兼顾了轻量级和实时性。代码开源：https:&#x2F;&#x2F;github.com&#x2F;Zwyywz&#x2F;laneDetector  1、canny边缘检测Canny边缘检测算法主要步骤：高斯滤波、梯度计算、非极大值抑制和双阈值检测。  高斯滤波：使用高斯滤波的目的是平滑图像，滤除图像中的部分噪声">
<meta property="og:type" content="article">
<meta property="og:title" content="基于OpenCV和C++车道线检测">
<meta property="og:url" content="https://gitee.com/zwyywz/zwyywz.git/2022/06/15/%E5%9F%BA%E4%BA%8EOpenCV%E5%92%8CC++%E8%BD%A6%E9%81%93%E7%BA%BF%E6%A3%80%E6%B5%8B/index.html">
<meta property="og:site_name" content="啊粥啊周舟の部落阁">
<meta property="og:description" content="基于OpenCV和C++车道线检前言： 这篇文章主要是一种基于边缘检测的传统车道线检测算法，这种算法完全依赖于OpenCV，兼顾了轻量级和实时性。代码开源：https:&#x2F;&#x2F;github.com&#x2F;Zwyywz&#x2F;laneDetector  1、canny边缘检测Canny边缘检测算法主要步骤：高斯滤波、梯度计算、非极大值抑制和双阈值检测。  高斯滤波：使用高斯滤波的目的是平滑图像，滤除图像中的部分噪声">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/weixin15937410430092-20230410105251467.png">
<meta property="article:published_time" content="2022-06-15T01:25:00.000Z">
<meta property="article:modified_time" content="2023-04-16T13:03:16.086Z">
<meta property="article:author" content="Zhouwy">
<meta property="article:tag" content="OpenCV">
<meta property="article:tag" content="machine vision">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/weixin15937410430092-20230410105251467.png"><link rel="shortcut icon" href="/zwyywz/img/favicon.png"><link rel="canonical" href="https://gitee.com/zwyywz/zwyywz.git/2022/06/15/%E5%9F%BA%E4%BA%8EOpenCV%E5%92%8CC++%E8%BD%A6%E9%81%93%E7%BA%BF%E6%A3%80%E6%B5%8B/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/zwyywz/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/zwyywz/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":300},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Zhouwy","link":"链接: ","source":"来源: 啊粥啊周舟の部落阁","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: true,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '基于OpenCV和C++车道线检测',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-16 21:03:16'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="///at.alicdn.com/t/c/font_4018630_wl3l75i04vl.css"><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/zwyywz/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/zwyywz/archives/"><div class="headline">文章</div><div class="length-num">17</div></a><a href="/zwyywz/tags/"><div class="headline">标签</div><div class="length-num">29</div></a><a href="/zwyywz/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/zwyywz/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/zwyywz/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/zwyywz/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zwyywz/note/"><i class="fa-fw iconfont icon-xuexibiji"></i><span> 学习笔记</span></a></li><li><a class="site-page child" href="/zwyywz/opensource/"><i class="fa-fw iconfont icon-xiangmu"></i><span> 开源项目</span></a></li><li><a class="site-page child" href="/zwyywz/development/"><i class="fa-fw iconfont icon-peizhi-xitongpeizhi"></i><span> 环境配置</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zwyywz/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/zwyywz/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/zwyywz/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/zwyywz/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://blog-1300216920.cos.ap-nanjing.myqcloud.com/weixin15937410430092-20230410105251467.png')"><nav id="nav"><span id="blog-info"><a href="/zwyywz/" title="啊粥啊周舟の部落阁"><span class="site-name">啊粥啊周舟の部落阁</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/zwyywz/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/zwyywz/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/zwyywz/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 分类</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zwyywz/note/"><i class="fa-fw iconfont icon-xuexibiji"></i><span> 学习笔记</span></a></li><li><a class="site-page child" href="/zwyywz/opensource/"><i class="fa-fw iconfont icon-xiangmu"></i><span> 开源项目</span></a></li><li><a class="site-page child" href="/zwyywz/development/"><i class="fa-fw iconfont icon-peizhi-xitongpeizhi"></i><span> 环境配置</span></a></li></ul></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/zwyywz/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/zwyywz/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/zwyywz/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/zwyywz/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">基于OpenCV和C++车道线检测</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-06-15T01:25:00.000Z" title="发表于 2022-06-15 09:25:00">2022-06-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-16T13:03:16.086Z" title="更新于 2023-04-16 21:03:16">2023-04-16</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="基于OpenCV和C++车道线检测"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="基于OpenCV和C-车道线检"><a href="#基于OpenCV和C-车道线检" class="headerlink" title="基于OpenCV和C++车道线检"></a>基于OpenCV和C++车道线检</h1><p>前言： 这篇文章主要是一种基于边缘检测的传统车道线检测算法，这种算法完全依赖于OpenCV，兼顾了轻量级和实时性。代码开源：<a target="_blank" rel="noopener" href="https://github.com/Zwyywz/laneDetector">https://github.com/Zwyywz/laneDetector</a></p>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/inputVideo.gif" alt="inputVideo"></p>
<h2 id="1、canny边缘检测"><a href="#1、canny边缘检测" class="headerlink" title="1、canny边缘检测"></a>1、canny边缘检测</h2><p><strong>Canny边缘检测算法主要步骤：高斯滤波、梯度计算、非极大值抑制和双阈值检测。</strong></p>
<ul>
<li>高斯滤波：使用高斯滤波的目的是平滑图像，滤除图像中的部分噪声（因为微分算子对噪声很敏感）</li>
<li>梯度计算：图像也可以计算梯度，由于数字图像是有离散的像素点的灰度值构成，所以微分运算就变成了差分，我们可以用相邻两个像素点之间的差分值表示该像素点在某个方向上灰度的变化情况。</li>
<li>非极大值抑制：细化边缘，梯度计算得到的边缘很粗，一条边缘中央一般很亮，两边亮度逐渐降低，可以根据这个特点去掉非局部灰度最高的“假边”，达到细化边缘的目的。</li>
<li>双阈值检测：减少伪边缘点非极大值抑制之后，检测到的边缘线条比较多，我们可以滤掉一些很暗的边缘，并让主要的边缘凸显出来。</li>
</ul>
<p><strong>OpenCV中提供了Canny边缘检测的方法，只需要调用对应的函数即可</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">cv::Mat <span class="title">doCanny</span><span class="params">(<span class="type">const</span> cv::Mat &amp;image)</span> </span>&#123;</span><br><span class="line">    <span class="comment">/* @function: 对输入图片做边缘检测，得到目标图片中突出的边缘线</span></span><br><span class="line"><span class="comment">     * @param: image -&gt; 输入图片</span></span><br><span class="line"><span class="comment">     * @return： canny -&gt; canny 边缘检测后的二值图</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    cv::Mat gray, gauss, canny;</span><br><span class="line">    cv::<span class="built_in">cvtColor</span>(image, gray, cv::COLOR_RGB2GRAY);</span><br><span class="line">    cv::<span class="built_in">GaussianBlur</span>(gray, gauss, cv::<span class="built_in">Size</span>(<span class="number">5</span>, <span class="number">5</span>), <span class="number">0</span>);</span><br><span class="line">    cv::<span class="built_in">Canny</span>(gauss, canny, <span class="number">50</span>, <span class="number">150</span>);</span><br><span class="line">    <span class="keyword">return</span> canny;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>Canny边缘检测后的结果如下：</p>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/canny.gif" alt="canny"></p>
<h2 id="2、图片分割"><a href="#2、图片分割" class="headerlink" title="2、图片分割"></a>2、图片分割</h2><p>图片分割的主要思想是，构建一个mask，这个mask包含主要的车道区域值均为1，其余为0，将这个mask与原frame进行叠加，就可以抠出主要的车道区域。</p>
<p>可以观察到：待识别的车道线主要分布于一个三角形区域内，我们可以估计这个三角形区域大概的顶点坐标，从而得到mask。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">cv::Mat <span class="title">doSegment</span><span class="params">(<span class="type">const</span> cv::Mat &amp;image)</span> </span>&#123;</span><br><span class="line">    <span class="comment">/* @function: 分割出车道线的区域</span></span><br><span class="line"><span class="comment">     * @param: image -&gt; 输入二值图</span></span><br><span class="line"><span class="comment">     * @return： 按points(3)中三点分割出三角形区域</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    <span class="type">int</span> height = image.rows;</span><br><span class="line">    cv::Mat segment;</span><br><span class="line">    <span class="function">std::vector&lt;cv::Point&gt; <span class="title">points</span><span class="params">(<span class="number">3</span>)</span></span>;</span><br><span class="line">    points[<span class="number">0</span>] = cv::<span class="built_in">Point</span>(<span class="number">0</span>, height);</span><br><span class="line">    points[<span class="number">1</span>] = cv::<span class="built_in">Point</span>(<span class="number">800</span>, height);</span><br><span class="line">    points[<span class="number">2</span>] = cv::<span class="built_in">Point</span>(<span class="number">380</span>, <span class="number">290</span>);</span><br><span class="line">    cv::Mat mask = cv::Mat::<span class="built_in">zeros</span>(image.rows, image.cols, CV_8UC1);</span><br><span class="line">    cv::<span class="built_in">fillPoly</span>(mask, points, <span class="number">255</span>);</span><br><span class="line">    cv::<span class="built_in">bitwise_and</span>(image, mask, segment);</span><br><span class="line">    <span class="keyword">return</span> segment;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/segment.gif" alt="segment"></p>
<h2 id="3、hough直线检测"><a href="#3、hough直线检测" class="headerlink" title="3、hough直线检测"></a>3、hough直线检测</h2><p>首先，介绍笛卡尔空间，就是我们常见的那个几何空间，通过 y=kx+b，可以表示直线。</p>
<p>然后，想一下，如果把上面方程变形一下，b=-xk+y，（k和b作为变量，x和y作为常量），那么是不是又是一条另外的直线呢？对了，这就是霍夫空间了。</p>
<ul>
<li>霍夫空间，笛卡尔空间中的直线，对应到霍夫空间中是一个点。</li>
<li>笛卡尔空间中共线的点，在霍夫空间中对应的直线相交。这个很重要，因为在笛卡尔空间中，我要做直线检测，岂不就是要找到最多的点所在的那条线嘛，我把所有的点都映射到霍夫空间中，找到最多的线公共交点就可以了。</li>
</ul>
<p>再然后，会发现一个问题，如果是一条垂直于x轴的直线，那么k岂不是正无穷，怎么办？</p>
<p>没关系，引进极坐标表示直线：ρ=xCosθ+ySinθ（ρ为原点到直线的距离）,如图所示</p>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/image-20220327160437866.png" alt=""></p>
<p>再然后，就可以将笛卡尔空间和霍夫空间做映射了。</p>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/20180329185654863.png" alt=""></p>
<p>继续然后，上面红色字体：找到最多的线公共交点就可以了，怎么找？</p>
<p>直接小白方式寻找，把霍夫空间网格化（就是一个很大的矩阵，初始值全是0），直线经过的地方标注1，没经过的地方还是0。再找出值最大的一些点就可以了。</p>
<p><strong>OpenCV中提供了Hough直线检测的方法，只需要调用对应的函数即可</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">cv::<span class="built_in">HoughLinesP</span>(segment, lines, <span class="number">2</span>, CV_PI / <span class="number">180</span>, <span class="number">100</span>, <span class="number">100</span>, <span class="number">50</span>);</span><br><span class="line">cv::<span class="built_in">line</span>(image,cv::<span class="built_in">Point2d</span>(lines[<span class="number">0</span>][<span class="number">0</span>],lines[<span class="number">0</span>][<span class="number">1</span>]),cv::<span class="built_in">Point2d</span>(lines[<span class="number">0</span>][<span class="number">2</span>],lines[<span class="number">0</span>][<span class="number">3</span>]),cv::<span class="built_in">Scalar</span>(<span class="number">0</span>,<span class="number">255</span>,<span class="number">0</span>),<span class="number">2</span>);</span><br><span class="line">cv::<span class="built_in">imshow</span>(<span class="string">&quot;Hough&quot;</span>,image);</span><br></pre></td></tr></table></figure>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/Hough.gif" alt="Hough"></p>
<p>不难观察出，由霍夫直线检测所得到的坐标点太多，导致检测出的直线左右闪动，所以我们要想办法将所有的坐标点变化量减小，所以平滑滤波？？？好吧！求个平均值就行！</p>
<h2 id="4、坐标计算"><a href="#4、坐标计算" class="headerlink" title="4、坐标计算"></a>4、坐标计算</h2><p>将从hough检测到的多条线平均成一条线表示车道的左边界， 一条线表示车道的右边界。基本思想很简单，<strong>就是先将霍夫变换的线段转换为一维信息，进行多项式拟合，在将得到的截距和斜率信息进行平均，在利用数值代换转换成cv坐标系的左边界线，和右边界线；</strong></p>
<p>首先，先实现一个多项式拟合函数，基本原理：<strong>幂函数可逼近任意函数。</strong></p>
<script type="math/tex; mode=display">
y = \sum_{i = 0}^{n}kx^i</script><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">cv::Mat <span class="title">polyfit</span><span class="params">(std::vector&lt;cv::Point&gt; &amp;in_point, <span class="type">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> size = in_point.<span class="built_in">size</span>();</span><br><span class="line">    <span class="comment">//所求未知数个数</span></span><br><span class="line">    <span class="type">int</span> x_num = n + <span class="number">1</span>;</span><br><span class="line">    <span class="comment">//构造矩阵U和Y</span></span><br><span class="line">    <span class="function">cv::Mat <span class="title">mat_u</span><span class="params">(size, x_num, CV_64F)</span></span>;</span><br><span class="line">    <span class="function">cv::Mat <span class="title">mat_y</span><span class="params">(size, <span class="number">1</span>, CV_64F)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; mat_u.rows; ++i)</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; mat_u.cols; ++j) &#123;</span><br><span class="line">            mat_u.<span class="built_in">at</span>&lt;<span class="type">double</span>&gt;(i, j) = <span class="built_in">pow</span>(in_point[i].x, j);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; mat_y.rows; ++i) &#123;</span><br><span class="line">        mat_y.<span class="built_in">at</span>&lt;<span class="type">double</span>&gt;(i, <span class="number">0</span>) = in_point[i].y;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//矩阵运算，获得系数矩阵K</span></span><br><span class="line">    <span class="function">cv::Mat <span class="title">mat_k</span><span class="params">(x_num, <span class="number">1</span>, CV_64F)</span></span>;</span><br><span class="line">    mat_k = (mat_u.<span class="built_in">t</span>() * mat_u).<span class="built_in">inv</span>() * mat_u.<span class="built_in">t</span>() * mat_y;</span><br><span class="line">    <span class="comment">//std::cout &lt;&lt; &quot;参数：&quot; &lt;&lt; mat_k &lt;&lt; std::endl;</span></span><br><span class="line">    <span class="keyword">return</span> mat_k;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们只需要得到斜率和截距，故而只需要拟合成一次多项式。</p>
<p>接下来正式处理hough直线检测后的坐标点：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">std::vector&lt;<span class="type">double</span>&gt; <span class="title">calculateLines</span><span class="params">(<span class="type">const</span> cv::Mat &amp;image, std::vector&lt;cv::Vec4i&gt; lines)</span> </span>&#123;</span><br><span class="line">    <span class="comment">/* @function: 将霍夫直线检测后得到的坐标进行处理，先做多项式拟合得到斜率和截距，再求斜率和截距平均值。</span></span><br><span class="line"><span class="comment">     * @param: image -&gt; 输入图片</span></span><br><span class="line"><span class="comment">     * @param: lines -&gt; 霍夫直线检测后得到的坐标</span></span><br><span class="line"><span class="comment">     * @return: 拟合后的左右两边的起始和终点坐标。</span></span><br><span class="line"><span class="comment">     *           std::vector&lt;double&gt; result = [x_left_start, y_left_start, x_left_end, y_left_end,x_right_start, y_right_start, x_right_end, y_right_end]</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    std::vector&lt;<span class="type">double</span>&gt; leftSlope, rightSlope, leftYIntercept, rightYIntercept;</span><br><span class="line">    <span class="keyword">for</span> (std::vector&lt;cv::Vec4i&gt;::iterator it = lines.<span class="built_in">begin</span>(); it &lt; lines.<span class="built_in">end</span>(); it++) &#123;</span><br><span class="line">        <span class="function">cv::Point2i <span class="title">startXY</span><span class="params">((*it)[<span class="number">0</span>], (*it)[<span class="number">1</span>])</span></span>;</span><br><span class="line">        <span class="function">cv::Point2i <span class="title">endXY</span><span class="params">((*it)[<span class="number">2</span>], (*it)[<span class="number">3</span>])</span></span>;</span><br><span class="line">        cv::Point in[<span class="number">2</span>] = &#123;startXY, endXY&#125;;</span><br><span class="line">        <span class="function">std::vector&lt;cv::Point&gt; <span class="title">in_point</span><span class="params">(std::begin(in), std::end(in))</span></span>;</span><br><span class="line">        cv::Mat parameters = <span class="built_in">polyfit</span>(in_point, <span class="number">1</span>);</span><br><span class="line">        <span class="type">double</span> y_intercept = parameters.<span class="built_in">at</span>&lt;<span class="type">double</span>&gt;(<span class="number">0</span>);</span><br><span class="line">        <span class="type">double</span> slope = parameters.<span class="built_in">at</span>&lt;<span class="type">double</span>&gt;(<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">if</span> (slope &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            leftSlope.<span class="built_in">emplace</span>(leftSlope.<span class="built_in">end</span>(), slope);</span><br><span class="line">            leftYIntercept.<span class="built_in">emplace</span>(leftYIntercept.<span class="built_in">end</span>(), y_intercept);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            rightSlope.<span class="built_in">emplace</span>(rightSlope.<span class="built_in">end</span>(), slope);</span><br><span class="line">            rightYIntercept.<span class="built_in">emplace</span>(rightYIntercept.<span class="built_in">end</span>(), y_intercept);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="type">double</span> leftSlopeAvg =</span><br><span class="line">            std::<span class="built_in">accumulate</span>(std::<span class="built_in">begin</span>(leftSlope), std::<span class="built_in">end</span>(leftSlope), <span class="built_in">double</span>(<span class="number">0.0</span>)) / <span class="built_in">double</span>(leftSlope.<span class="built_in">size</span>());</span><br><span class="line">    <span class="type">double</span> rightSlopeAvg =</span><br><span class="line">            std::<span class="built_in">accumulate</span>(std::<span class="built_in">begin</span>(rightSlope), std::<span class="built_in">end</span>(rightSlope), <span class="built_in">double</span>(<span class="number">0.0</span>)) / <span class="built_in">double</span>(rightSlope.<span class="built_in">size</span>());</span><br><span class="line">    <span class="type">double</span> leftYInterceptAvg =</span><br><span class="line">            std::<span class="built_in">accumulate</span>(std::<span class="built_in">begin</span>(leftYIntercept), std::<span class="built_in">end</span>(leftYIntercept), <span class="built_in">double</span>(<span class="number">0.0</span>)) /</span><br><span class="line">            <span class="built_in">double</span>(leftYIntercept.<span class="built_in">size</span>());</span><br><span class="line">    <span class="type">double</span> rightYInterceptAvg =</span><br><span class="line">            std::<span class="built_in">accumulate</span>(std::<span class="built_in">begin</span>(rightYIntercept), std::<span class="built_in">end</span>(rightYIntercept), <span class="built_in">double</span>(<span class="number">0.0</span>)) /</span><br><span class="line">            <span class="built_in">double</span>(rightYIntercept.<span class="built_in">size</span>());</span><br><span class="line"></span><br><span class="line">    std::vector&lt;<span class="type">double</span>&gt; left_line = <span class="built_in">calculate_coordinates</span>(image, leftSlopeAvg, leftYInterceptAvg);</span><br><span class="line">    std::vector&lt;<span class="type">double</span>&gt; right_line = <span class="built_in">calculate_coordinates</span>(image, rightSlopeAvg, rightYInterceptAvg);</span><br><span class="line">    std::vector&lt;<span class="type">double</span>&gt; result;</span><br><span class="line">    result.<span class="built_in">resize</span>(left_line.<span class="built_in">size</span>() + right_line.<span class="built_in">size</span>());</span><br><span class="line">    std::<span class="built_in">merge</span>(left_line.<span class="built_in">begin</span>(), left_line.<span class="built_in">end</span>(), right_line.<span class="built_in">begin</span>(), right_line.<span class="built_in">end</span>(), result.<span class="built_in">begin</span>());</span><br><span class="line"><span class="comment">//    std::cout &lt;&lt; &quot;calculateLines: &quot;;</span></span><br><span class="line"><span class="comment">//    for (std::vector&lt;double&gt;::iterator it = result.begin(); it &lt; result.end(); ++it) &#123;</span></span><br><span class="line"><span class="comment">//        std::cout &lt;&lt; *it &lt;&lt; &quot; &quot;;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line"><span class="comment">//    std::cout &lt;&lt; std::endl;</span></span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>结果如下：</p>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/output.gif" alt="output"></p>
<h2 id="5、结果可视化"><a href="#5、结果可视化" class="headerlink" title="5、结果可视化"></a>5、结果可视化</h2><p>最后，将得到的坐标通过OpenCV中画图工具即可叠加到原图上：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function">cv::Mat <span class="title">visualize_lines</span><span class="params">(<span class="type">const</span> cv::Mat &amp;frame, std::vector&lt;<span class="type">double</span>&gt; coordinates)</span> </span>&#123;</span><br><span class="line">    <span class="comment">/* @function: 将直线画在与frame大小相同的mask上</span></span><br><span class="line"><span class="comment">     * @param: frame -&gt; 输入图片</span></span><br><span class="line"><span class="comment">     * @param: coordinates -&gt; 坐标：</span></span><br><span class="line"><span class="comment">     *                std::vector&lt;double&gt; coordinates = [x_left_start, y_left_start, x_left_end, y_left_end,x_right_start, y_right_start, x_right_end, y_right_end]</span></span><br><span class="line"><span class="comment">     * @return: 画上直线的掩码mask</span></span><br><span class="line"><span class="comment">     * */</span></span><br><span class="line">    cv::Mat mask = cv::Mat::<span class="built_in">zeros</span>(frame.rows, frame.cols, CV_8UC3);</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">unsigned</span> <span class="type">long</span> i = <span class="number">0</span>; i &lt; coordinates.<span class="built_in">size</span>(); i += <span class="number">4</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (i &lt; <span class="number">4</span>)</span><br><span class="line">            cv::<span class="built_in">line</span>(mask, cv::<span class="built_in">Point2d</span>(coordinates[i], coordinates[i + <span class="number">1</span>]),</span><br><span class="line">                     cv::<span class="built_in">Point2d</span>(coordinates[i + <span class="number">2</span>], coordinates[i + <span class="number">3</span>]), cv::<span class="built_in">Scalar</span>(<span class="number">0</span>, <span class="number">255</span>, <span class="number">0</span>), <span class="number">3</span>);</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">            cv::<span class="built_in">line</span>(mask, cv::<span class="built_in">Point2d</span>(coordinates[i], coordinates[i + <span class="number">1</span>]),</span><br><span class="line">                     cv::<span class="built_in">Point2d</span>(coordinates[i + <span class="number">2</span>], coordinates[i + <span class="number">3</span>]), cv::<span class="built_in">Scalar</span>(<span class="number">255</span>, <span class="number">0</span>, <span class="number">0</span>), <span class="number">3</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> mask;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/result.gif" alt="result"></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/zwyywz/zwyywz.git">Zhouwy</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/zwyywz/zwyywz.git/2022/06/15/%E5%9F%BA%E4%BA%8EOpenCV%E5%92%8CC++%E8%BD%A6%E9%81%93%E7%BA%BF%E6%A3%80%E6%B5%8B/">https://gitee.com/zwyywz/zwyywz.git/2022/06/15/%E5%9F%BA%E4%BA%8EOpenCV%E5%92%8CC++%E8%BD%A6%E9%81%93%E7%BA%BF%E6%A3%80%E6%B5%8B/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/zwyywz/zwyywz.git" target="_blank">啊粥啊周舟の部落阁</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/zwyywz/tags/OpenCV/">OpenCV</a><a class="post-meta__tags" href="/zwyywz/tags/machine-vision/">machine vision</a></div><div class="post_share"><div class="social-share" data-image="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/weixin15937410430092-20230410105251467.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/zwyywz/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/zwyywz/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/zwyywz/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/zwyywz/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/zwyywz/2022/06/15/%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%88%86%E7%B1%BB%E5%9B%BE%E7%89%87/" title="机器视觉的算法研究"><img class="cover" src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/t.png" onerror="onerror=null;src='/zwyywz/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">机器视觉的算法研究</div></div></a></div><div class="next-post pull-right"><a href="/zwyywz/2022/05/22/TensorRT%E6%95%99%E7%A8%8B/" title="TensorRT实战教程"><img class="cover" src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/FI-1.png" onerror="onerror=null;src='/zwyywz/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">TensorRT实战教程</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/zwyywz/2020/04/26/DigitalImageProcessing/" title="数字图像处理（DIP）"><img class="cover" src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/DIP.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-26</div><div class="title">数字图像处理（DIP）</div></div></a></div><div><a href="/zwyywz/2023/01/15/zkViewYolo/" title="针对zkview平台上嵌入机器视觉算法的框架"><img class="cover" src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/image-20230410111343844.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-15</div><div class="title">针对zkview平台上嵌入机器视觉算法的框架</div></div></a></div><div><a href="/zwyywz/2021/06/15/%E5%9F%BA%E4%BA%8E%E6%9C%BA%E5%99%A8%E8%A7%86%E8%A7%89%E7%9A%84%E4%BB%BF%E7%94%9F%E6%9C%BA%E6%A2%B0%E6%89%8B%E6%8E%8C/" title="基于机器视觉的机械手掌"><img class="cover" src="https://blog-1300216920.cos.ap-nanjing.myqcloud.com/yolo.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-06-15</div><div class="title">基于机器视觉的机械手掌</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E4%BA%8EOpenCV%E5%92%8CC-%E8%BD%A6%E9%81%93%E7%BA%BF%E6%A3%80"><span class="toc-number">1.</span> <span class="toc-text">基于OpenCV和C++车道线检</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81canny%E8%BE%B9%E7%BC%98%E6%A3%80%E6%B5%8B"><span class="toc-number">1.1.</span> <span class="toc-text">1、canny边缘检测</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E5%9B%BE%E7%89%87%E5%88%86%E5%89%B2"><span class="toc-number">1.2.</span> <span class="toc-text">2、图片分割</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81hough%E7%9B%B4%E7%BA%BF%E6%A3%80%E6%B5%8B"><span class="toc-number">1.3.</span> <span class="toc-text">3、hough直线检测</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%9D%90%E6%A0%87%E8%AE%A1%E7%AE%97"><span class="toc-number">1.4.</span> <span class="toc-text">4、坐标计算</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E7%BB%93%E6%9E%9C%E5%8F%AF%E8%A7%86%E5%8C%96"><span class="toc-number">1.5.</span> <span class="toc-text">5、结果可视化</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Zhouwy</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/zwyywz/js/utils.js"></script><script src="/zwyywz/js/main.js"></script><script src="/zwyywz/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typesetPromise()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>